package com.duke.shaking.profile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.DatePicker;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.duke.shaking.BaseActivity;
import com.duke.shaking.Constants;
import com.duke.shaking.R;
import com.duke.shaking.ShakeTabActivity;
import com.duke.shaking.ShakeTabEvent;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.net.NetUtils;
import com.duke.shaking.pojo.User;
import com.duke.shaking.pojo.UserImage;
import com.duke.shaking.utils.AppUtils;
import com.duke.shaking.utils.PhotoUtils;
import com.duke.shaking.widget.FormRow;
import com.duke.shaking.widget.PhotoMgrGridAdapter;
import com.duke.shaking.widget.PhotoMgrGridView;
import com.duke.shaking.widget.StartTextEditActivityHandler;
import com.loopj.android.http.AsyncHttpClient;

public class MyselfActivity extends BaseActivity 
		implements ShakeTabEvent, OnDateSetListener{

	private final int DATE_DIALOG_ID = 1;
	
	private User editUser;
	
	//private FormRow row_user_id;
	private FormRow row_reg_info;
	//private FormRow row_social;
	private FormRow row_nickname;
	private FormRow row_birth;
	private FormRow row_school;
	private FormRow row_career;
	private FormRow row_hobbies;
	
	private PhotoMgrGridView photoGridView;
	private int currPos;
//	private View gridView;
	private TextView txtAgeAndLocation;
	private TextView txtSign;
	
	private AQuery aQuery;
	private Context context;
//	private UploadThread uploadThread;
	private ProgressDialog progress;
	
	private StartTextEditActivityHandler handler = new StartTextEditActivityHandler(this);
	
	public MyselfActivity() {
		super();
		aQuery = new AQuery(this);
	}

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myself);
        
        context = this.getApplicationContext();
        
        editUser = new User();
        
        if (me != null) {
        	/*editUser.setUserId(me.getUserId());
        	editUser.setNickname(me.getNickname());
        	editUser.setBirth(me.getBirth());
        	editUser.setSchool(me.getSchool());
        	editUser.setAvatarUrl(me.getAvatarUrl());
        	editUser.setJob(me.getJob());
        	editUser.setSex(me.getSex());
        	if (me.getHobbies() != null) {
        		editUser.setHobbies(new ArrayList<Integer>(me.getHobbies()));
        	}*/
        	setVipIcon(me);
        	editUser = me;
        	initAgeAndLocationText(editUser);
        }
        initRowsByUser(editUser);
        
        photoGridView = (PhotoMgrGridView)this.findViewById(R.id.photoGridView);
        photoGridView.setEditable(true);
        initPhotos();
        
        photoGridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				currPos = position;
				if(photoGridView.isEditButton(position)) {
					fetchPhoto();
				} else {
					// launch gallery activity to view all photos
					Intent i = new Intent(context, ViewPhotosPagerActivity.class);
					i.putExtra(ViewPhotosPagerActivity.EXTRA_IMAGES, photoGridView.getPhotos());
					i.putExtra(ViewPhotosPagerActivity.EXTRA_POSITION, position);
					startActivity(i);
				}
			}
		});
        
        photoGridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(!photoGridView.isEditButton(position)) {
					editPhoto(position);
				}
				return false;
			}
		});
	}
	
	private void setVipIcon(User me) {
		View vip = findViewById(R.id.img_vip);
    	View vipYear = findViewById(R.id.img_vipyear);
    	if (me == null || me.getLevel() == null || me.getLevel() <= 0) {
    		if (vip != null) {
    			vip.setVisibility(View.GONE);
    		}
    		if (vipYear != null) {
    			vipYear.setVisibility(View.GONE);
    		}
    	}
    	else if (me.getLevel() < User.level_vip_year) {
    		if (vip != null) {
    			vip.setVisibility(View.VISIBLE);
    		}
    		if (vipYear != null) {
    			vipYear.setVisibility(View.GONE);
    		}
    	}
    	else {
    		if (vip != null) {
    			vip.setVisibility(View.GONE);
    		}
    		if (vipYear != null) {
    			vipYear.setVisibility(View.VISIBLE);
    		}
    	}
	}
	
	private void initAgeAndLocationText(User editUser) {
		if (txtAgeAndLocation == null) {
			txtAgeAndLocation = (TextView)findViewById(R.id.txt_age_and_location);
		}
		if (txtAgeAndLocation != null) {
			int age = 0;
			if (editUser.getBirth() != null) {
				Date now = new Date();
				age = (int)((now.getTime() - editUser.getBirth().getTime()) / 1000L / (24 * 3600 * 365));
			}
			String str = String.format(Locale.CHINA,
					getString(R.string.fmt_age_and_location),
					age,
					"",
					(editUser.getSex() == User.sex_female ? 
							getString(R.string.female) : 
							getString(R.string.male)));
			txtAgeAndLocation.setText(str);
		}
	}
	
	private void initPhotos() {
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				super.onResultSuccess(response);
				
				JSONObject jsonBody = getBody(response);
				if(jsonBody != null) {
					try {
						JSONArray photos = jsonBody.optJSONArray("photo");
						List<UserImage> imgList = new ArrayList<UserImage>();
						if(photos != null) {
							// reverse the order
							for(int i = photos.length() - 1; i >= 0 ; i --) {
								String pid = photos.getJSONObject(i).optString("id");
								String url = photos.getJSONObject(i).optString("url");
								String url_1= photos.getJSONObject(i).optString("urlm");
								imgList.add(new UserImage(url, url_1, pid));
							}
						}
						photoGridView.addPhotos(imgList);
						updatePhotos();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

        	@Override
        	public void onResultFailure(String result) {
        		super.onResultFailure(result);

        	}
		};


		Map<String, String> params = new HashMap<String, String>();
		params.put(NetUtils.TOKEN, me.getAuthToken());
		NetUtils.getPhotos(this, responseHandler, params);
	}

	public void fetchPhoto() {
		DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				switch(which) {
				case 0:
					gotoGallery();
					break;
				case 1:
					gotoTakePhoto();
					break;
				}
			}
		};
		
		new AlertDialog.Builder(this)
			.setTitle(R.string.title_select_photo_src)
			.setItems(R.array.item_dlg_select_photo_src, 
					listener)
			.show();
	}
	
	public void editPhoto(final int position) {
		DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				switch(which) {
				case 0:
					setAsAvatar(position);
					break;
				case 1:
					deletePhoto(position);
					break;
				}
			}
		};
		
		new AlertDialog.Builder(this)
			.setTitle(R.string.title_select_photo_src)
			.setItems(R.array.item_dlg_edit_photo, 
					listener)
			.show();
	}
	
	public void setAsAvatar(int position) {
		UserImage userImage = photoGridView.getPhoto(position);
        if(userImage != null && userImage.getUrl() != null) {
        	Intent i = new Intent(this, ImageCropActivity.class);
        	i.putExtra("networkUrl", userImage.getUrl());
        	startActivityForResult(i, Constants.INTENT_CODE_GET_AVATAR);
        }
	}
	
	public void deletePhoto(final int position) {
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				super.onResultSuccess(response);
				
				photoGridView.removePhoto(position);
				updatePhotos();
			}

			@Override
			public void onResultFailure(String result) {
				super.onResultFailure(result);
				toast(R.string.err_delete_photo);
			}
        };
        

        UserImage userImage = photoGridView.getPhoto(position);
        if(userImage != null) {
        	Map<String, String> params = new HashMap<String, String>();
        	params.put(NetUtils.TOKEN, me.getAuthToken());
        	params.put(NetUtils.PID, userImage.getPid());
        	AsyncHttpClient client = NetUtils.deletePhoto(this, responseHandler, params);
        	showProgress(R.string.progress_delete_photo, client, responseHandler);
        }
	}

	public void gotoTakePhoto() {
		Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        //i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
		startActivityForResult(i, Constants.INTENT_CODE_TAKE_PHOTO);
	}

	public void gotoGallery() {
		Intent i = getGalleryPickIntent();
		startActivityForResult(i, Constants.INTENT_CODE_FROM_GALLERY);
	}
	
	private Intent getGalleryPickIntent() {  
        Intent intent = new Intent(Intent.ACTION_PICK, null);  
        intent.setType("image/*");
        //intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 80);
        intent.putExtra("outputY", 80);
        intent.putExtra("return-data", true);
        return intent;  
    }
	
	public void onTakePhotoResult(int resultCode, Intent intent) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		
		Uri uri = intent.getData();
		if (uri == null) {
			error("get image from camera failed!");
			return;
		}
		addBitmap(uri);
	}
	
	public void onFromGalleryResult(int resultCode, Intent intent) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		Uri uri = intent.getData();
		if (uri == null) {
			error("get image from gallery failed!");
			return;
		}
		
		info("get image from gallery: " + uri.toString());
       	addBitmap(uri);
	}
	
	/*private class UploadThread extends Thread {
		private Map<String, String> params;
		private int type;
		
		public UploadThread(Map<String, String> params, 
				int type) {
			this.params = params;
			this.type = type;
			
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String re = NetUtils.uploadPhoto(context, type, params);
			Message message = new Message();
			message.arg1 = type;
			if(re == null) {
				message.what = 0;
				toast(R.string.err_upload_photo);
			} else {
				message.what = 1;
				message.obj = re;
				//onUploadPhotoSuccess(re);
			}
			handler2.sendMessage(message);
			Log.i(TAG, "upload photo result: " + re);
		}
	}*/
	

	private void uploadPhoto(final String photoPath, final int type) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NetUtils.TOKEN, me.getAuthToken());
		params.put(NetUtils.UPFILE, photoPath);
		//NetUtils.uploadPhoto(context, NetUtils.PHOTO_ALBUM, params, responseHandler);
		int msgId;
		if (type == NetUtils.PHOTO_AVATAR) {
			msgId = R.string.progress_upload_avatar;
		}
		else {
			msgId = R.string.progress_upload_photo;
		}
		
		/*uploadThread = new UploadThread(params, type);
		uploadThread.start();*/
		
		AjaxCallback<JSONObject> ajaxCallback = new AjaxCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject object,
					AjaxStatus status) {
				Log.i(TAG, "JSONObject: " + object.toString());
				if (type == NetUtils.PHOTO_ALBUM) {
					onUploadPhotoSuccess(object);
				} else if(type == NetUtils.PHOTO_AVATAR) {
					me.setAvatarUrl(photoPath);
				}
				dismissProgressDlg();
			}

			@Override
			public void failure(int code, String message) {
				dismissProgressDlg();
				if (type == NetUtils.PHOTO_AVATAR) {
					toast(R.string.err_set_avatar);
				}
				else  {
					toast(R.string.err_upload_photo);
				}
			}
			
		};
		showProgressDlg(msgId);
		NetUtils.uploadPhoto(context, type, params, ajaxCallback);
	}
	
	private void showProgressDlg(int msgId) {
		progress = new ProgressDialog(this);
		progress.setCanceledOnTouchOutside(false);
		progress.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				onCancelProgressDlg();
			}
			
		});
		
		if (!progress.isShowing()){
			progress.setTitle(null);
			progress.setMessage(getString(msgId));
			progress.setCancelable(true);
			progress.show();
		}
	}
	
	private void onCancelProgressDlg() {
		AjaxCallback.cancel();
		dismissProgressDlg();
	}
	
	private void dismissProgressDlg() {
		if(progress != null && progress.isShowing()) {
			progress.dismiss();
		}
	}
	
	@Override
	protected void onStop() {
		onCancelProgressDialog();
		super.onStop();
	}

	protected void addBitmap(final Uri imageUri) {
		
		/*AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				super.onResultSuccess(response);
				
				// TODO: GET URL AND PID
				JSONObject jsonBody = getBody(response);
				Log.i(TAG, "upload photo response: " + response);
				String imageUrl = "http://192.168.0.106/shake/" + (photoGridView.getTotalCount() + 1) + ".png";
				
				// TODO: get the upload url path and
				UserImage newImage = new UserImage(imageUrl, "2");
				photoGridView.addPhoto(newImage);
				updatePhotos();
			}

        	@Override
        	public void onResultFailure(String result) {
        		super.onResultFailure(result);

        	}
		};*/
		
		// save photo to hard storage
		String photoPath = PhotoUtils.savePhotoForUpload(imageUri, me.getTelNo());
		uploadPhoto(photoPath, NetUtils.PHOTO_ALBUM);
		
	}
	
	/*Handler handler2 = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (progress != null && progress.isShowing()) {
				progress.dismiss();
			}
			int type = msg.arg1;
			if(msg.what == 1) {
				if (type == NetUtils.PHOTO_AVATAR) {
					toast(R.string.set_avatar_successfully);
				}
				else {
					String re = (String)msg.obj;
					onUploadPhotoSuccess(re);
					toast(R.string.photo_upload_successfully);
				}
			}
			else {
				if (type == NetUtils.PHOTO_AVATAR) {
					toast(R.string.err_set_avatar);
				}
				else  {
					toast(R.string.err_upload_photo);
				}
			}
		}
		
	};*/
	
	/*
	 * Callback method after image uploading success
	 */
	private void onUploadPhotoSuccess(String result) {
		try {
			JSONObject jsonObj = new JSONObject(result);
			onUploadPhotoSuccess(jsonObj);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void onUploadPhotoSuccess(JSONObject jsonObj) {
		String pid = jsonObj.optString("pid");
		String url = jsonObj.optString("url");
		String url_1 = jsonObj.optString("url_1");
		String url_5 = jsonObj.optString("url_5");
		String url_6 = jsonObj.optString("url_6");
		
		UserImage newImage = new UserImage(url, url_1, url_5, url_6, pid);
		photoGridView.addPhoto(newImage);
		updatePhotos();
	}

	private void initRowsByUser(User user) {
		txtSign = (TextView)findViewById(R.id.txt_sign);
		if (txtSign != null) {
			txtSign.setText(user.getSignature() == null ? "" : user.getSignature());
		}
		
		/*row_user_id = (FormRow)findViewById(R.id.row_user_id);
		if (row_user_id != null) row_user_id.setText(
				user.getUserId() == null ? "" : user.getUserId());
		*/
		row_reg_info = (FormRow)findViewById(R.id.row_reg_info);
		if (row_reg_info != null) row_reg_info.setText(
				user.getRegDate() == null ? "" : AppUtils.getDateString(user.getRegDate()));
		
		//row_social = (FormRow)findViewById(R.id.row_social);
		
		row_nickname = (FormRow)findViewById(R.id.row_nickname);
		if (row_nickname != null) row_nickname.setText(
				user.getNickname() == null ? "" : user.getNickname());
		
		row_birth = (FormRow)findViewById(R.id.row_birth);
		if (row_birth != null) {
			Date birth = user.getBirth();
			setBirthRow(birth);
		}
		
		row_school = (FormRow)findViewById(R.id.row_school);
		if (row_school != null) row_school.setText(
				user.getSchool() == null ? "" : user.getSchool());
		
		row_career = (FormRow)findViewById(R.id.row_career);
		if (row_career != null && user.getJob() != null) row_career.setText(
				(user.getJob() < User.MIN_JOB || user.getJob() > User.MAX_JOB) ? "" : 
					this.getResources().getStringArray(R.array.careers)[user.getJob()]);
		
		row_hobbies = (FormRow)findViewById(R.id.row_hobbies);
		if(row_hobbies != null) {
			List<Integer> hobbyList = user.getHobbies();
			StringBuilder str = new StringBuilder();
			
			if (hobbyList != null) {
				for (Integer h : hobbyList) {
					str.append(this.getResources().getStringArray(
							R.array.hobbyArray)[h]);
					str.append(" ");
				}
			}
			String strHobbies = str.toString().trim();
			row_hobbies.setText(strHobbies);
		}
	}
	
	public void onActive(ShakeTabActivity tabs) {
		// TODO Auto-generated method stub
		tabs.initTitle(0, //ShakeTabActivity.TITLE_BTN_EXIST | ShakeTabActivity.TITLE_BTN_BACK,
				0, //R.string.back,
				0,
				0,
				R.string.title_myself);
	}

	public void onBackBtn() {
		// TODO Auto-generated method stub
		
	}

	public void onLeftBtn() {
		// TODO Auto-generated method stub
		
	}

	public void onRightBtn() {
		// TODO Auto-generated method stub
		onSubmit(editUser);
	}
	
	public void onNicknameEdit(View v) {
		Message msg = new Message();
		msg.what = Constants.INTENT_CODE_EDIT_NICKNAME;
		msg.obj = editUser.getNickname();
		handler.sendMessage(msg);
	}
	
	public void onSignEdit(View v) {
		Message msg = new Message();
		msg.what = Constants.INTENT_CODE_EDIT_SIGNATURE;
		msg.obj = editUser.getSignature();
		handler.sendMessage(msg);
	}
	
	public void onBirthEdit(View v) {
		showDialog(DATE_DIALOG_ID);
	}
	
	protected Dialog onCreateDialog(int id) {
		if (id == DATE_DIALOG_ID) {
			if (editUser.getBirth() != null) {
				return new DatePickerDialog(this, this, 
					editUser.getBirth().getYear() + 1900,
					editUser.getBirth().getMonth(),
					editUser.getBirth().getDate());
			}
			else {
				return new DatePickerDialog(this, this, 
						1986,
						5,
						1);
			}
		}
		return null;
	}
	
	public void onCareerEdit(View v) {
		Intent i = new Intent(this, CareerEditActivity.class);
		i.putExtra("career", editUser.getJob());
		startActivityForResult(i, Constants.INTENT_CODE_EDIT_CAREER);
	}
	
	public void onSchoolEdit(View v) {
		Message msg = new Message();
		msg.what = Constants.INTENT_CODE_EDIT_SCHOOL;
		msg.obj = editUser.getSchool();
		handler.sendMessage(msg);
	}
	
	public void onHobbiesEdit(View v) {
		Intent i = new Intent(this, HobbiesEditActivity.class);
		ArrayList<Integer> hobbies = (ArrayList<Integer>)editUser.getHobbies();
		i.putExtra("hobbies", hobbies);
		startActivityForResult(i, Constants.INTENT_CODE_EDIT_HOBBIES);
	}
	
	public void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		
		switch(requestCode) {
		case Constants.INTENT_CODE_EDIT_NICKNAME: {
			String input = intent.getStringExtra("input");
			if (input != null) {
				row_nickname.setText(input);
				editUser.setNickname(input);
			}
			break;
		}
		case Constants.INTENT_CODE_EDIT_SCHOOL: {
			String input = intent.getStringExtra("input");
			if (input != null) {
				row_school.setText(input);
				editUser.setSchool(input);
			}
			break;
		}
		case Constants.INTENT_CODE_EDIT_CAREER:
			int career = intent.getIntExtra("career", 0);
			editUser.setJob(career);
			if(career > 0) {
				String careerName = intent.getStringExtra("careerName");
				if (careerName != null) {
					row_career.setText(careerName);
				}
			}
			else {
				row_career.setText("");
			}
			break;
		case Constants.INTENT_CODE_EDIT_HOBBIES:
			ArrayList<Integer> hobbies = (ArrayList<Integer>)intent.getSerializableExtra("hobbies");
			if (hobbies == null) {
				editUser.setHobbies(null);
			}
			
			String hobbiesName = intent.getStringExtra("hobbiesName");
			if (hobbiesName != null) {
				row_hobbies.setText(hobbiesName);
			}
			break;
		case Constants.INTENT_CODE_TAKE_PHOTO:
			onTakePhotoResult(resultCode, intent);
			break;
		case Constants.INTENT_CODE_FROM_GALLERY:
			onFromGalleryResult(resultCode, intent);
			break;
		case Constants.INTENT_CODE_EDIT_SIGNATURE: {
			String input = intent.getStringExtra("input");
			if (input != null) {
				if (txtSign != null) {
					txtSign.setText(input);
				}
				editUser.setSignature(input);
			}
			break;
		}
		case Constants.INTENT_CODE_GET_AVATAR: {
			onGetAvatarResult(resultCode, intent);
			break;
		}
		}
		
	}
	
	private void submitAvatar(String path) {
		
        uploadPhoto(path, NetUtils.PHOTO_AVATAR);
	}

	public void onGetAvatarResult(int resultCode, Intent intent) {
		Uri imgUri = intent.getData();
		if (imgUri == null) {
			toast(R.string.err_set_avatar);
			return;
		}
		
		String path = intent.getStringExtra("cropImagePath");
		if (path == null) {
			toast(R.string.err_set_avatar);
			return;
		}

		submitAvatar(path);
	}
	
	private void onSubmit(User user) {
		
	}

	public void onDateSet(DatePicker picker, int year, int monthOfYear, int dayOfMonth) {
		// TODO Auto-generated method stub
		Date birthday = new Date(year - 1900, monthOfYear, dayOfMonth);
		saveBirthday(birthday);
		/*editUser.setBirth(birthday);
		setBirthRow(birthday);*/
	}
	
	private void setBirthRow(Date birth) {
		if(birth != null) {
			String strDate = AppUtils.getDateString(birth);
			String zo = AppUtils.getZodiac(birth);
			String strCon = AppUtils.getConstellation(birth);
			row_birth.setText(String.format(
					getString(R.string.fnt_birth), strDate, zo, strCon));
		} else {
			row_birth.setText("");
		}
	}
	
	private void saveBirthday(final Date date) {
		AsyncHttpResponseWithResultHandler responseHandler = new AsyncHttpResponseWithResultHandler(){
			
        	@Override
			public void onResultSuccess(String response) {
				// TODO Auto-generated method stub
				super.onResultSuccess(response);
				editUser.setBirth(date);
				setBirthRow(date);
			}

			@Override
			public void onResultFailure(String result) {
				// TODO Auto-generated method stub
				super.onResultFailure(result);
			}
        };
        
        Map<String, String> params = new HashMap<String, String>();
        params.put(NetUtils.BIRTHDAY, new SimpleDateFormat("yyyy-MM-dd").format(date));
        AsyncHttpClient client = NetUtils.updateProfile(this, responseHandler, params, null);
        if (client != null) {
        	showProgress(R.string.progress_update_profile, client, responseHandler);
        }
	}
	
	public void updatePhotos() {
		photoGridView.updateHeight();
		((PhotoMgrGridAdapter)photoGridView.getAdapter()).notifyDataSetChanged();
	}
}
